package cn.cque.service.impl;

import cn.cque.dao.CategoryDao;
import cn.cque.entity.Category;
import cn.cque.service.CategoryService;
import cn.cque.utils.JedisUtils;
import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: XWC
 * @ClassName: CategoryServiceImpl
 * @Date: 2020/10/4
 * @Time: 18:38
 * @Description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Override
    public List<Category> findAll() {
        /*//先去Redis数据库查询
        Jedis jedis= JedisUtils.getJedis();
        String categoryList = jedis.get("categoryList");

        Gson gson = new Gson();
        List<Category> categories;

        //如果Redis数据库不存在数据
        if (!StringUtils.isEmpty(categoryList)){
            categories = gson.fromJson(categoryList, new TypeToken<List<Category>>() {
            }.getType());
        }else {
            categories = categoryDao.findAll();
            //将数据加入Redis数据库
            String json = gson.toJson(categories);
            jedis.set("categoryList",json);
        }
        jedis.close();*/

        return categoryDao.findAll();
    }

    @Override
    public Category findById(String cid) {
        return categoryDao.findById(cid);
    }


    @Override
    @Transactional
    public Integer addCategory(Category category) {
        return categoryDao.addCategory(category);
    }

    @Override
    @Transactional
    public Integer deleteCategory(String cid) {
        return categoryDao.deleteCategory(cid);
    }

    @Override
    @Transactional
    public Integer updateCategory(Category category) {
        return categoryDao.updateCategory(category);
    }
}